package com.bz.xtcx.manager.mapper.provider;

import com.bz.xtcx.manager.persist.conditions.LibCondition;
import com.bz.xtcx.manager.utils.QuerySplitUtil;
import com.bz.xtcx.manager.utils.WzStrUtil;
import com.bz.xtcx.manager.vo.VoQuery;
import org.springframework.util.StringUtils;

import java.util.List;

public class LibReqProvider {

    public String findByConditionApi(LibCondition qc) {
        StringBuilder sql = new StringBuilder("select l.id, l.req_name, l.number, l.unit_name, l.domain, l.country, l.req_desc, l.address, l.req_new, l.req_other, i.field1 as logo ");
        if(qc.isLoadAll()) {
            sql.append(", l.req_background, l.req_get ");
        }
        sql.append(" from `lib_requirements` l left join `lib_image` i on l.id=i.lib_id ");
        sql.append("where l.status = 1 ");
        if(!StringUtils.isEmpty(qc.getObjName())) {

            List<String> paramList = QuerySplitUtil.getParamList(qc.getObjName());
            sql.append(" and (");
            for (int i = 0; i < paramList.size(); i++) {
                String param = paramList.get(i);
                if(i != 0){
                    sql.append(" or ");
                }
                sql.append(" l.req_name like '%").append(param).append("%' ");
                sql.append(" or l.code like '%").append(param).append("%' ");
                sql.append(" or l.unit_name like '%").append(param).append("%' ");
                sql.append(" or l.number like '%").append(param).append("%' ");
                sql.append(" or l.domain like '%").append(param).append("%' ");
                sql.append(" or l.search_param like '%").append(param).append("%' ");
                sql.append(" or l.req_desc like '%").append(param).append("%' ");
                sql.append(" or l.req_new like '%").append(param).append("%' ");
                sql.append(" or l.req_get like '%").append(param).append("%' ");
            }
            sql.append(")");
        }
        if (WzStrUtil.isNotBlank(qc.getObjCountry())) {
            sql.append(" and l.country like '%").append(qc.getObjCountry()).append("%' ");
        }
        if (WzStrUtil.isNotBlank(qc.getObjDomain())) {
            sql.append(" and l.domain like '%").append(qc.getObjDomain()).append("%' ");
        }
        if (WzStrUtil.isNotBlank(qc.getProvince())) {
            sql.append(" and l.search_param like '%").append(qc.getProvince()).append("%' ");
        }
        if (WzStrUtil.isNotBlank(qc.getCity())) {
            sql.append(" and l.search_param like '%").append(qc.getCity()).append("%' ");
        }
        if (WzStrUtil.isNotBlank(qc.getZone())) {
            sql.append(" and l.search_param like '%").append(qc.getZone()).append("%' ");
        }
        // add by wangtao on 201901301600 for 门户资源列表倒序 start
        sql.append(" order by l.update_time desc, l.req_name ");
        // add by wangtao on 201901301600 for 门户资源列表倒序 end
        return sql.toString();
    }

    public String findByCondition(VoQuery e) {
        StringBuilder sql = new StringBuilder("select * from `lib_requirements` where 1=1 ");
        sql.append(queryCondition(e));
        return sql.toString();
    }
    
    StringBuilder queryCondition(VoQuery e) {
        StringBuilder sql = new StringBuilder();
        if(e != null) {
            if(e.getUserId() != null) {
                sql.append(" and user_id = '").append(e.getUserId()).append("'");
            }
            if (!StringUtils.isEmpty(e.getObjName())) {

                List<String> paramList = QuerySplitUtil.getParamList(e.getObjName());
                sql.append(" and (");
                for (int i = 0; i < paramList.size(); i++) {
                    String param = paramList.get(i);
                    if(i != 0){
                        sql.append(" or ");
                    }
                    sql.append(" req_name like '%"+param+"%' ");
                    sql.append(" or number like '%"+param+"%' ");
                    sql.append(" or search_param like '%").append(param).append("%' ");
                    sql.append(" or code like '%").append(param).append("%' ");
                    sql.append(" or unit_name like '%").append(param).append("%' ");
                    sql.append(" or domain like '%").append(param).append("%' ");
                    sql.append(" or req_desc like '%").append(param).append("%' ");
                    sql.append(" or req_new like '%").append(param).append("%' ");
                    sql.append(" or req_get like '%").append(param).append("%' ");
                }
                sql.append(")");
            }
            if (!StringUtils.isEmpty(e.getStatus())) {
                sql.append(" and status = '"+e.getStatus()+"' ");
            }
            if (WzStrUtil.isNotBlank(e.getObjCountry())) {
                sql.append(" and country like '%").append(e.getObjCountry()).append("%' ");
            }
            if (WzStrUtil.isNotBlank(e.getObjDomain())) {
                sql.append(" and domain like '%").append(e.getObjDomain()).append("%' ");
            }
            if (WzStrUtil.isNotBlank(e.getProvince())) {
                sql.append(" and search_param like '%").append(e.getProvince()).append("%' ");
            }
            if (WzStrUtil.isNotBlank(e.getCity())) {
                sql.append(" and search_param like '%").append(e.getCity()).append("%' ");
            }
            if (WzStrUtil.isNotBlank(e.getZone())) {
                sql.append(" and search_param like '%").append(e.getZone()).append("%' ");
            }
        }
        return sql;
    }
}
